# -*- coding: utf-8 -*-
"""
Callbacks for sorting querysets.

Usage::

    - the callback takes 2 arguments: the field itself and the queryset to
      manipulate 
    - the callback must return the manipulated queryset
"""
from .. import utils, settings

multicallbacks = utils.multicallbacks

def from_fields(*args):
    """
    Takes as *args field names that are just passed as *queryset.order_by()* 
    args.
    """
    def callback(field, objects):
        prefix = "" if field.direction == settings.ASC else "-"
        orders = list(args)
        orders[0] = u"%s%s" % (prefix, args[0])
        return objects.order_by(*orders)
    return callback



